/// 路由器
import { scanRoutes, setupRouter } from '@expresso/plus'
import Main from '@/layouts/Main/Index'
import { cache } from '@/helpers'

/** 路由器 */
const router = setupRouter([
  {
    name: 'login',
    path: '/login',
    component: () => import('@/layouts/Login/Index'),
  },
  {
    name: 'main',
    path: '/',
    component: Main,
    children: scanRoutes(
      import.meta.glob(
        [
          './routes/**/*.routes.ts',
        ],
        { eager: true },
      ),
    ),
  },
  {
    path: '/redirect',
    component: Main,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/layouts/Redirect/Index'),
      },
    ],
  },
])

/* 路由拦截 */
router.beforeEach((to, from, next) => {
  if (!['/login'].includes(to.path.toLocaleLowerCase())) {
    if (!cache.getToken()) {
      next({ path: '/login', replace: true })
    }
  }
  next()
})

export default router
